package cn.it.ygh.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import cn.it.ygh.annotation.SystemLog;
import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.HouseManageService;

@Controller
@RequestMapping("/houseManage")
public class HouseManageController {
	
	@Resource
	private HouseManageService houseManageService;
	
	@RequestMapping("/getAllHouse.do")
	public ModelAndView getAllHouse()
	{
		ModelAndView modelAndView = new ModelAndView();
		List<House> houseList = houseManageService.getAllHouse();
		modelAndView.addObject("houseList",houseList);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
	@RequestMapping("/getHouseById.do")
	@SystemLog(description="查看房屋的详细信息")
	public ModelAndView getHouseById(@RequestParam(value="houseId") int houseId,HttpSession session)
	{
		ModelAndView modelAndView = new ModelAndView();
		House house = houseManageService.getHouseById(houseId);
		session.setAttribute("temp", house);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
	@RequestMapping("/deleteHouse.do")
	@SystemLog(description="删除房屋信息")
	public ModelAndView deleteHouse(@RequestParam(value="houseId") int houseId)
	{
		ModelAndView modelAndView = new ModelAndView();
		houseManageService.deleteHouse(houseId);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
	@RequestMapping("/batchdeleteHouse.do")
	@SystemLog(description="批量删除房屋信息")
	public ModelAndView batchdeleteHouse(@RequestParam(value="houseIds") String houseIds)
	{
		ModelAndView modelAndView = new ModelAndView();
		String str[] = houseIds.split("#");
		int[] ids = new int[str.length];
		for(int i = 0;i < str.length;i++)
			ids[i] = Integer.parseInt(str[i]);
		houseManageService.batchdeleteHouse(ids);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
	@RequestMapping(value="/addHouse.do",method=RequestMethod.POST)
	@SystemLog(description="添加房屋信息")
	public ModelAndView addHouse(House house){
		System.out.println("----");
		ModelAndView modelAndView = new ModelAndView();
		//user.setUserTime(new Date());
		houseManageService.addHouse(house);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/findHouse.do",method=RequestMethod.POST)
	public ModelAndView findHouse(@RequestParam(value = "key") String key)
	{
		ModelAndView modelAndView = new ModelAndView();
		List<House> houseList = houseManageService.findHouse(key);
		modelAndView.addObject("houseList", houseList);
		modelAndView.setViewName("../admin/jsp/houses-list");
		return modelAndView;
	}
}
